#!/bin/bash

set -o errexit
set -o xtrace

test_dir=$(realpath $(dirname $0))
. ${test_dir}/../functions

create_infra $namespace
desc 'create first PXC cluster'
cluster="some-name"
spinup_pxc "$cluster" "$conf_dir/$cluster.yml"

desc 'pause'
cat_config "$conf_dir/$cluster.yml" \
    | sed -e 's/pause: false/pause: true/' \
    | kubectl_bin apply -f-
wait_for_delete pod/$cluster-proxysql-0
wait_for_delete pod/$cluster-pxc-2
wait_for_delete pod/$cluster-pxc-1
wait_for_delete pod/$cluster-pxc-0

desc 'unpause, check data'
cat_config "$conf_dir/$cluster.yml" \
    | sed -e 's/pause: true/pause: false/' \
    | kubectl_bin apply -f-
wait_for_running "$cluster-proxysql" 1
wait_for_running "$cluster-pxc" 3

compare_mysql_cmd "select-1" "SELECT * from myApp.myApp;" "-h $cluster-pxc-0.$cluster-pxc -uroot -proot_password"
compare_mysql_cmd "select-1" "SELECT * from myApp.myApp;" "-h $cluster-pxc-1.$cluster-pxc -uroot -proot_password"
compare_mysql_cmd "select-1" "SELECT * from myApp.myApp;" "-h $cluster-pxc-2.$cluster-pxc -uroot -proot_password"
run_mysql \
    'INSERT myApp.myApp (id) VALUES (100501)' \
    "-h $cluster-proxysql -uroot -proot_password"
compare_mysql_cmd "select-2" "SELECT * from myApp.myApp;" "-h $cluster-pxc-0.$cluster-pxc -uroot -proot_password"
compare_mysql_cmd "select-2" "SELECT * from myApp.myApp;" "-h $cluster-pxc-1.$cluster-pxc -uroot -proot_password"
compare_mysql_cmd "select-2" "SELECT * from myApp.myApp;" "-h $cluster-pxc-2.$cluster-pxc -uroot -proot_password"

desc 'delete cluster'
kubectl_bin delete \
    -f $conf_dir/$cluster.yml
wait_for_delete pod/$cluster-pxc-2
wait_for_delete pod/$cluster-pxc-1
wait_for_delete pod/$cluster-pxc-0

desc 'recreate cluster, check data'
cat_config "$conf_dir/$cluster.yml" \
    | sed -e 's/storage: 2Gi/storage: 3Gi/; s/size: 3/size: 5/; s/antiAffinityTopologyKey:.*/antiAffinityTopologyKey: none/' \
    | kubectl_bin apply -f-
wait_for_running "$cluster-proxysql" 1
wait_for_running "$cluster-pxc" 5

compare_mysql_cmd "select-2" "SELECT * from myApp.myApp;" "-h $cluster-pxc-0.$cluster-pxc -uroot -proot_password"
compare_mysql_cmd "select-2" "SELECT * from myApp.myApp;" "-h $cluster-pxc-1.$cluster-pxc -uroot -proot_password"
compare_mysql_cmd "select-2" "SELECT * from myApp.myApp;" "-h $cluster-pxc-2.$cluster-pxc -uroot -proot_password"
compare_mysql_cmd "select-2" "SELECT * from myApp.myApp;" "-h $cluster-pxc-3.$cluster-pxc -uroot -proot_password"
compare_mysql_cmd "select-2" "SELECT * from myApp.myApp;" "-h $cluster-pxc-4.$cluster-pxc -uroot -proot_password"

desc 'write data to ProxySQL, read from all'
run_mysql \
    'INSERT myApp.myApp (id) VALUES (100502)' \
    "-h $cluster-proxysql -uroot -proot_password"
compare_mysql_cmd "select-3" "SELECT * from myApp.myApp;" "-h $cluster-pxc-0.$cluster-pxc -uroot -proot_password"
compare_mysql_cmd "select-3" "SELECT * from myApp.myApp;" "-h $cluster-pxc-1.$cluster-pxc -uroot -proot_password"
compare_mysql_cmd "select-3" "SELECT * from myApp.myApp;" "-h $cluster-pxc-2.$cluster-pxc -uroot -proot_password"
compare_mysql_cmd "select-3" "SELECT * from myApp.myApp;" "-h $cluster-pxc-3.$cluster-pxc -uroot -proot_password"
compare_mysql_cmd "select-3" "SELECT * from myApp.myApp;" "-h $cluster-pxc-4.$cluster-pxc -uroot -proot_password"

destroy $namespace
